<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="活动" prop="taskId">
        <el-input v-model="queryParams.titleImg" placeholder="请输入缩略图" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="活动ID" prop="taskId">
        <el-input v-model="queryParams.taskId" placeholder="请输入活动ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:buzNews:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:buzNews:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:buzNews:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:buzNews:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="buzNewsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="缩略图" align="center" prop="titleImg">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.titleImg" fit="contain" v-if="scope.row.titleImg"></el-image>
          <div style="width: 10px;height: 10px;" v-else></div>
        </template>
      </el-table-column>
      <el-table-column label="所属活动" align="center" prop="taskId" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:buzNews:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:buzNews:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改学习资讯对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :key="xxzxKey">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="缩略图" prop="titleImg">
          <el-upload style="border: 0px solid red;width: 178px;height: 178px;" class="avatar-uploader"
            :show-file-list="false" :action="uploadurl" :headers="{ Authorization: 'Bearer ' + token }" name="imgfile"
            :on-success="uploadFileSuccess">
            <img v-if="form.titleImg" :src="form.titleImg" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="视频链接" prop="vedioUrl">
          <el-input v-model="form.vedioUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="内容" prop="context">
          <Editor v-model="form.context" :height="178"></Editor>
        </el-form-item>
        <el-form-item label="活动ID" prop="taskId">
          <el-select v-model="form.taskId" placeholder="请选择活动ID" clearable style="width: 100%">
            <el-option v-for="(dict, index) in taskList" :key="dict.id" :label="dict.title" :value="dict.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBuzNews, getBuzNews, delBuzNews, addBuzNews, updateBuzNews, getTaskList } from "@/api/system/buzNews";
import { getToken } from '@/utils/auth'
export default {
  name: "BuzNews",
  data() {
    return {
      uploadurl: process.env.VUE_APP_BASE_API + '/system/buzTask/uploadImg',
      token: getToken(),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学习园地表格数据
      buzNewsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        titleImg: null,
        vedioUrl: null,
        context: null,
        taskId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      taskList: [],
      xxzxKey:Math.random()
    };
  },
  created() {
    this.getList();
    this.getTaskList()
  },
  methods: {
    getTaskList() {
      getTaskList().then(response => {
        this.taskList = response.data || [];
      });
    },
    /** 查询学习园地列表 */
    getList() {
      this.loading = true;
      listBuzNews(this.queryParams).then(response => {
        this.buzNewsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        titleImg: null,
        vedioUrl: null,
        context: null,
        taskId: null,
        status: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.xxzxKey = Math.random()
      this.reset();
      this.open = true;
      this.title = "添加学习资讯";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBuzNews(id).then(response => {
        this.xxzxKey = Math.random()
        this.form = response.data;
        this.open = true;
        this.title = "添加学习资讯";
      });
    },
    uploadFileSuccess(res, file) {
      if (res.code === 200) {
        this.form.titleImg = res.imgUrl;
      } else {
        this.$message({
          type: 'error',
          message: res.msg
        })
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBuzNews(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuzNews(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学习园地编号为"' + ids + '"的数据项？').then(function () {
        return delBuzNews(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/buzNews/export', {
        ...this.queryParams
      }, `buzNews_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  /* 178px */
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
